<?php
    /**
     * Created by PhpStorm.
     * User: long
     * Date: 2018/9/28
     * Time: 下午5:59
     */

    namespace app\member\home;

    use app\common\utils\UtilEncryption;
    use app\common\utils\UtilJwt;
    use app\face2market\model\Face2marketRecommendRewardMsgModel;
    use app\member\logic\Member;
    use app\member\model\MemberInfoModel;
    use app\member\model\MemberModel;
    use think\Cache;
    use think\Controller;

    class Userlogin extends Controller
    {

        protected function check_validate($data,$type)
        {

            $result = $this->validate($data,$type);
            (true !== $result) ? $this->error($result) : null;
        }

        protected function check_validate_param($type)
        {

            $data   = $this->request->param();
            $result = $this->validate($data,$type);
            (true !== $result) ? $this->error($result) : $data;
        }

        /**
         * 信息展示页面
         *
         * @return mixed
         */
        public function index()
        {

            $data = $this->request->param();

            return $this->assign('data',$data)->fetch('login');

        }

        /**
         * 信息展示页面
         *
         * @return mixed
         */
        public function recommend_msg()
        {


            $data = $this->request->param();
            $key  = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAQEP73sZ8VLb1zcJJOPnkJrIziI8WoxxslARPplWxA5/VZltGoJPile3Qe56nKRQx2VeOv3zdVc3ta8iPAC5XTLA0R6L/7N6oHKU6EYapJOJeJ1T+TXqOT9N6UncJ+vUd7Ju/SYtvCCas07FbnpS6bq5wwIDAQA';

            if (empty($data['id']))
            {
                $data['id'] = '';
            }
            $member_id = UtilJwt::decode($data['id'],$key);

            if ($data['id'] == '' || $member_id == false)
            {
                $this->redirect('Userlogin/index');
            }


            $list = Face2marketRecommendRewardMsgModel::share_type(1,$member_id,$arr = []);


            $info['recommendtotaltree'] = Face2marketRecommendRewardMsgModel::recommend_msg_sum($member_id);
            $info['one']                = MemberModel::where(['phone_puid' => $member_id])->count();
            $info['effective']          = Face2marketRecommendRewardMsgModel::recommend_msg_count($member_id);
            $info['invalid']            = $info['one'] - $info['effective'];
            $lists['list']              = array_merge($list);
            $lists['info']              = $info;
            return $this->assign('data',$lists)->fetch('register');

        }

        /**
         * 信息展示页面
         *
         * @return mixed
         */
        public function login()
        {

            $data = $this->request->param();
            // 验证
            $this->check_validate($data,'Auth.login');
            if (empty($data['referrer_phone']))
            {
                $data['referrer_phone'] = '';
            }

            $phone = $data['phone'];
            $pwd   = $data['password'];

            $pwd = UtilEncryption::encryptMd5($pwd,config('member_pwd_key'));


            $member_info = MemberModel::field('id,phone,password,status')->where([
                'phone'    => $phone,
                'password' => $pwd
            ])->find();

            if ($member_info)
            {
                if ($member_info['status'] == 0)
                {
                    $this->error('账号已冻结，请联系客服！');
                }


                $member = MemberInfoModel::getMemberInfo($member_info['id']);

                $member['token'] = UtilJwt::encode([
                    'member_id'   => $member_info->id,
                    'phone'       => $member_info->phone,
                    'create_time' => time()
                ],config('JwtKey'));
                $key             = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAQEP73sZ8VLb1zcJJOPnkJrIziI8WoxxslARPplWxA5/VZltGoJPile3Qe56nKRQx2VeOv3zdVc3ta8iPAC5XTLA0R6L/7N6oHKU6EYapJOJeJ1T+TXqOT9N6UncJ+vUd7Ju/SYtvCCas07FbnpS6bq5wwIDAQA';

                $member['referrer_phone'] = $data['referrer_phone'];
                $member['id']             = UtilJwt::encode($member['id'],$key);
                $this->success('登陆成功！','',$member);
            }
            else
            {
                $this->error('用户不存在或密码错误！');
            }


        }

    }